package com.roy.rocketmq.controller;

import com.alibaba.fastjson.JSON;
import com.roy.rocketmq.basic.MyProducer;
import com.roy.rocketmq.basic.SpringProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ：楼兰
 * @date ：Created in 2020/10/27
 * @description:
 **/
@RestController
@RequestMapping("/MQTest")
public class MQTestController {

    private final String topic = "TestTopic";
    @Resource
    private SpringProducer producer;


    @Autowired
    MyProducer myProducer;

    @RequestMapping("/sendMessage")
    public String sendMessage(String message) {
        producer.sendMessage(topic, message);
        return "消息发送完成";
    }

    //这个发送事务消息的例子中有很多问题，需要注意下。
    @RequestMapping("/sendTransactionMessage")
    public String sendTransactionMessage(String message) throws InterruptedException {
        producer.sendMessageInTransaction(topic, message);
        return "消息发送完成";
    }


    /**
     * 发送同步字符串消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/syncSendStr")
    public String syncSendStr(String message) throws InterruptedException {
        SendResult sendResult = myProducer.syncSendStr();
        return JSON.toJSONString(sendResult);
    }


    /**
     * 发送同步Object消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/syncSendObject")
    public String syncSendObject(String message) throws InterruptedException {
        myProducer.syncSendObject();
        return "";
    }

    /**
     * 发送同步Object消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/syncSendMessage")
    public String syncSendMessage(String message) throws InterruptedException {
        myProducer.syncSendMessage();
        return "";
    }

    /**
     * 发送异步消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/asyncSend")
    public String asyncSend(String message) throws InterruptedException {
        myProducer.asyncSend();
        return "";
    }


    /**
     * 发送指定TAG的消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/convertAndSend")
    public String convertAndSend(String message) throws InterruptedException {
        myProducer.convertAndSend();
        return "";
    }

    /**
     * 异步发送消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/asyncSendAndReceive")
    public String asyncSendAndReceive(String message) throws InterruptedException {
        myProducer.asyncSendAndReceive();
        return "";
    }
    /**
     * 批量
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/sendBatch")
    public String sendBatch(String message) throws InterruptedException {
        myProducer.sendBatch();
        return "";
    }
    /**
     * 延迟消息
     *
     * @param message
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("/sendScheduled")
    public String sendScheduled(String message) throws InterruptedException {
        myProducer.sendScheduled();
        return "";
    }
}